<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>节点</title>
    <style>
        /* li a {
            float: right;
        } */
        
        table {
            width: 500px;
            margin: 100px auto;
            border-collapse: collapse;
            text-align: center;
        }
        
        td,
        th {
            border: 1px solid #333;
        }
        
        thead tr {
            height: 40px;
            background-color: #ccc;
        }
    </style>
</head>

<body>
    <!-- <textarea name="" id="" cols="30" rows="10"></textarea>
    <input class="button" type="submit" name="" id="" value="发布">
    <ul></ul>
    <script>
        var text = document.querySelector('textarea');
        var btn = document.querySelector('.button');
        var ul = document.querySelector('ul');

        btn.onclick = function() {
            if (text.value == '') {
                alert('请输入内容');
                return false;

            } else {
                var li = document.createElement('li');
                li.innerHTML = text.value + "<a herf:'javascript:;'>删除</a>"; //javascript:;可以阻止页面跳转

                //ul.appendChild(li);这是在后面添加，可以用insertBefore将文本添加到前面
                ul.insertBefore(li, ul.children[0]); //添加在前面时，必须要指定位置
                var as = document.querySelectorAll('a');
                text.value = '';
                for (var i = 0; i < as.length; i++) {
                    as[i].onclick = function() {
                        ul.removeChild(this.parentNode); //ul是要删除的这个li的父亲，this.parentNode是a标签的父类，注意这个坑
                    }
                }
            }
        }
    </script> -->
    <!-- 用JavaScript创建一个表格 -->
    <table>
        <thead>
            <tr>
                <th>姓名</th>
                <th>科目</th>
                <th>成绩</th>
                <th>删除</th>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
    <ul></ul>
    <script>
        var datas = [{
            name: '张三',
            sbj: 'javascript',
            score: 100
        }, {
            name: '李四',
            sbj: 'javascript',
            score: 98
        }, {
            name: '王五',
            sbj: 'javascript',
            score: 90
        }, {
            name: '赵四',
            sbj: 'javascript',
            score: 99
        }, {
            name: '大猪蹄子',
            sbj: 'javascript',
            score: 0
        }];
        var tbody = document.querySelector('tbody');
        for (var i = 0; i < datas.length; i++) {
            var tr = document.createElement('tr'); //创建行，有多少个对象就有多少行
            //创建列
            for (var k in datas[i]) { //对象里有多少个属性就生成多少个列
                var td = document.createElement('td');
                td.innerHTML = datas[i][k];
                tr.appendChild(td);
            }
            tbody.appendChild(tr);
            //创建第二个单元格
            var td = document.createElement('td');
            td.innerHTML = '<a href="javascript:;"> 删除</a>';
            tr.appendChild(td);
        }
        var as = document.querySelectorAll('a');

        for (var i = 0; i < as.length; i++) {
            as[i].onclick = function() {
                tbody.removeChild(this.parentNode.parentNode); //一定要找好父节点是谁
            }
        }
    </script>
</body>

</html>